package com.julian.mvc.djview.model;

import com.julian.mvc.djview.view.BPMObserver;
import com.julian.mvc.djview.view.BeatObserver;

import java.util.ArrayList;
import java.util.Random;

public class HeartModel implements HeartModelInterface, Runnable {
    ArrayList beatObservers = new ArrayList();
    ArrayList bpmObservers = new ArrayList();
    int time = 1000;
    int bpm = 90;
    Random random = new Random(System.currentTimeMillis());
    Thread thread;

    public HeartModel() {
        thread = new Thread(this);
        thread.start();
    }

    @Override
    public int getHeartRate() {
        return 60000 / time;
    }

    @Override
    public void registerObserver(BeatObserver observer) {
        beatObservers.add(observer);
    }

    @Override
    public void removeObserver(BeatObserver observer) {
        beatObservers.remove(observer);
    }

    @Override
    public void registerObserver(BPMObserver observer) {
        bpmObservers.add(observer);
    }

    @Override
    public void removeObserver(BPMObserver observer) {
        bpmObservers.add(observer);
    }

    @Override
    public void run() {
        int lastrate = -1;

        for (;;){
            int change = random.nextInt(10);
            if (random.nextInt(2) == 0){
                change = 0 - change;
            }
            int rate = 60000/ (time + change);
            if (rate < 120 && rate > 50){
                time += change;
                notifyBeatObservers();
                if (rate != lastrate){
                    lastrate = rate;
                    notifyBPMObservers();
                }
            }
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void notifyBPMObservers() {
        for (int i = 0; i < beatObservers.size(); i++){
            BPMObserver observer = (BPMObserver)bpmObservers.get(i);
            observer.updateBPM();
        }
    }

    private void notifyBeatObservers() {
        for (int i = 0; i < beatObservers.size(); i++){
            BeatObserver observer = (BeatObserver)beatObservers.get(i);
            observer.updateBeat();
        }
    }
}

